﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using ZedGraph;
using System.Threading;
using SerialportSample;
//using HFSoft.Component.Windows;
namespace Config
{
    public partial class OutForm : Form
    {
        private const int N = 42;
        double[] freq = new double[N];
        double[] Rxy = new double[N];
        double[] RxyERR = new double[N];
        double[] RxyERRmin = new double[N];
        double[] RxyERRmax = new double[N];
        double[] Ryx = new double[N];
        double[] RyxERR = new double[N];
        double[] RyxERRmin = new double[N];
        double[] RyxERRmax = new double[N];
        double[] Phaxy = new double[N];
        double[] PhaxyERR = new double[N];
        double[] PhaxyERRmin = new double[N];
        double[] PhaxyERRmax = new double[N];
        double[] Phayx = new double[N];
        double[] PhayxERR = new double[N];
        double[] PhayxERRmin = new double[N];
        double[] PhayxERRmax = new double[N];
        public delegate void MyInvoke();//线程相关
        private SerialportSampleForm ssf;
        private string path;
        Load load; 
        public OutForm(SerialportSampleForm s,string path)
        {
            InitializeComponent();
            changeTxt();
            ssf = s;
            this.path = path;
           // path = ssf.getPath()+ssf.getFile2();
            load = new Load(this); 
            Thread thread = new Thread(new ThreadStart(DoWork));
            thread.Start();
            
            
        }
        public void changeTxt()
        {
            GraphPane myPane1 = zedGraphControl1.GraphPane;
            GraphPane myPane2 = zedGraphControl2.GraphPane;
            myPane1.Title.Text = "Apparent Resistivity(Ω.m)";
            myPane1.XAxis.Title.Text = "lg(Period/s)";
            myPane1.YAxis.Title.Text = "Amplitude";
            //myPane1.Y2Axis.Title.Text = "Acceleration, m/s2";

            myPane2.Title.Text = "Impedance Phase(°)";
            myPane2.XAxis.Title.Text = "lg(Period)";
            myPane2.YAxis.Title.Text = "Degree";
        }
        public void UpdateForm()
        {
            DrawCurve1();
            DrawCurve2(); 
            load.Hide();
           // callClose();
        }

        private void callShow(){
            if (this.IsHandleCreated) {
                MethodInvoker mi = new MethodInvoker(this.ShowForm);
                this.BeginInvoke(mi);
            }

        }

        private void ShowForm( )
        {   
            load.ShowDialog();
         }
        private void callClose()
        {
            if (this.IsHandleCreated)
            {
                MethodInvoker mi = new MethodInvoker(this.CloseForm);
                this.BeginInvoke(mi);
            }
        }

        private void CloseForm()
        {
            Thread.Sleep(3000);
            load.Hide();
        }
        public void DoWork()
        {
          
            callShow(); 
            MyInvoke mi = new MyInvoke(UpdateForm);
            StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read));
            int k;
           
            string str1 = sr.ReadLine();//用于读取第一行，标题，舍掉
            for (k = 0; k < N; k++)
            {
                string str = sr.ReadLine();
                if (str == null) break;
                string[] sArray = Regex.Split(str, " ", RegexOptions.IgnoreCase);
                
                int j = 0;
                double[] data = new double[30];
                foreach (string i in sArray)
                {
                    string trim = i.Trim();
                    if (trim.Length != 0)
                    {
                        data[j] = Convert.ToDouble(trim);
                        j++;
                    }

                }
                /*
                freq[k] = data[0];
                freq[k] = Math.Round(Math.Log(freq[k], 10), 2);//对浮点数去对数后再保留两位小数
                Rxy[k] = data[13];
                Rxy[k] = Math.Round(Math.Log(Rxy[k], 10), 2);
                RxyERR[k] = data[17];
                RxyERR[k] = Math.Round(Math.Log(RxyERR[k], 10), 2);
                RxyERRmin[k] = Rxy[k] - RxyERR[k] / 2;//分别计算该点处误差棒的最下端和最上端
                RxyERRmax[k] = Rxy[k] + RxyERR[k] / 2;
                Ryx[k] = data[14];
                Ryx[k] = Math.Round(Math.Log(Ryx[k], 10), 2);
                RyxERR[k] = data[18];
                RyxERR[k] = Math.Round(Math.Log(RyxERR[k], 10), 2);
                RyxERRmin[k] = Ryx[k] - RyxERR[k] / 2;
                RyxERRmax[k] = Ryx[k] + RyxERR[k] / 2;
                Phaxy[k] = data[15];
                Phaxy[k] = Math.Round(Phaxy[k], 2);
       
                PhaxyERR[k] = data[19];
                PhaxyERR[k] = Math.Round(PhaxyERR[k], 2);
                PhaxyERRmin[k] = Phaxy[k] - PhaxyERR[k] / 2;
                PhaxyERRmax[k] = Phaxy[k] + PhaxyERR[k] / 2;
                //System.Console.WriteLine(PhaxyERR[k]);
                Phayx[k] = data[16];
                Phayx[k] = Math.Round(Phayx[k], 2);
      
                PhayxERR[k] = data[20];
                PhayxERR[k] = Math.Round(PhayxERR[k], 2);
                PhayxERRmin[k] = Phayx[k] - PhayxERR[k] / 2;
                PhayxERRmax[k] = Phayx[k] + PhayxERR[k] / 2;
                 * */
                freq[k] = data[0]; 
                freq[k] = Math.Round(freq[k], 2);
                //freq[k] = Math.Round(Math.Log(freq[k], 10), 2);//对浮点数去对数后再保留两位小数

                Rxy[k] = data[13];
                Rxy[k] = Math.Round(Rxy[k], 2);
                RxyERR[k] = data[17]; 
                RxyERR[k] = Math.Round(RxyERR[k], 2);     
                RxyERRmin[k] = Rxy[k] - RxyERR[k] / 2;//分别计算该点处误差棒的最下端和最上端
                RxyERRmax[k] = Rxy[k] + RxyERR[k] / 2;
                //Rxy[k] = Math.Round(Math.Log(Rxy[k], 10), 2);
                //RxyERR[k] = Math.Round(Math.Log(RxyERR[k], 10), 2);
               // RxyERRmin[k] = Math.Round(Math.Log(RxyERRmin[k], 10), 2);
               // RxyERRmax[k] = Math.Round(Math.Log(RxyERRmax[k], 10), 2);

                Ryx[k] = data[14];             
                RyxERR[k] = data[18];         
                RyxERRmin[k] = Ryx[k] - RyxERR[k] / 2;
                RyxERRmax[k] = Ryx[k] + RyxERR[k] / 2;
                //Ryx[k] = Math.Round(Math.Log(Ryx[k], 10), 2);
               // RyxERR[k] = Math.Round(Math.Log(RyxERR[k], 10), 2);
               // RyxERRmin[k] = Math.Round(Math.Log(RyxERRmin[k], 10), 2);
               // RyxERRmax[k] = Math.Round(Math.Log(RyxERRmax[k], 10), 2);

                Phaxy[k] = data[15];
                Phaxy[k] = Math.Round(Phaxy[k], 2);
                PhaxyERR[k] = data[19];
                PhaxyERR[k] = Math.Round(PhaxyERR[k], 2);
                PhaxyERRmin[k] = Phaxy[k] - PhaxyERR[k] / 2;
                PhaxyERRmax[k] = Phaxy[k] + PhaxyERR[k] / 2;
                //System.Console.WriteLine(PhaxyERR[k]);
                Phayx[k] = data[16];
                Phayx[k] = Math.Round(Phayx[k], 2);

                PhayxERR[k] = data[20];
                PhayxERR[k] = Math.Round(PhayxERR[k], 2);
                PhayxERRmin[k] = Phayx[k] - PhayxERR[k] / 2;
                PhayxERRmax[k] = Phayx[k] + PhayxERR[k] / 2;
            }
            sr.Close();
            this.BeginInvoke(mi); 
            //callClose();
           
        }
       
        private void DrawCurve1()
        {
            ZedGraph.PointPairList list1 = new ZedGraph.PointPairList();
            ZedGraph.PointPairList list11 = new ZedGraph.PointPairList();
            ZedGraph.LineItem myCurve1;
            ZedGraph.ErrorBarItem myCurve11;

            ZedGraph.PointPairList list2 = new ZedGraph.PointPairList();
            ZedGraph.PointPairList list21 = new ZedGraph.PointPairList();
            ZedGraph.LineItem myCurve2;
            ZedGraph.ErrorBarItem myCurve21;

           // zedGraphControl1.GraphPane.YAxis.Scale.Min = 0;
            zedGraphControl1.GraphPane.YAxis.Scale.Min = 0.01f;

            list11.Add(freq,RxyERRmin, RxyERRmax);
            list1.Add(freq, Rxy);
            myCurve11 = zedGraphControl1.GraphPane.AddErrorBar("RxyERR", list11, Color.DarkGreen);
            myCurve1 = zedGraphControl1.GraphPane.AddCurve("Rxy", list1, Color.DarkGreen, ZedGraph.SymbolType.TriangleDown);
            //myCurve1.GetXAxis(zedGraphControl1.GraphPane).MinSpace = 10;
            myCurve1.GetXAxis(zedGraphControl1.GraphPane).Type = AxisType.Log;
            myCurve11.GetYAxis(zedGraphControl1.GraphPane).Type = AxisType.Log;
            list21.Add(freq, RyxERRmin, RyxERRmax);
            list2.Add(freq, Ryx);
            myCurve21 = zedGraphControl1.GraphPane.AddErrorBar("RyxERR", list21, Color.Red);
            myCurve2 = zedGraphControl1.GraphPane.AddCurve("Ryx", list2, Color.Red, ZedGraph.SymbolType.Triangle);
            //myCurve2.GetXAxis(zedGraphControl1.GraphPane).MinSpace = 10;
            myCurve2.GetXAxis(zedGraphControl1.GraphPane).Type = AxisType.Log;
            myCurve21.GetYAxis(zedGraphControl1.GraphPane).Type = AxisType.Log;
            zedGraphControl1.AxisChange();
            zedGraphControl1.Refresh();
            
        }
        private void DrawCurve2()
        {
            ZedGraph.PointPairList list1 = new ZedGraph.PointPairList();
            ZedGraph.PointPairList list11 = new ZedGraph.PointPairList();
            ZedGraph.LineItem myCurve1;
            ZedGraph.ErrorBarItem myCurve11;

            ZedGraph.PointPairList list2 = new ZedGraph.PointPairList();
            ZedGraph.PointPairList list21 = new ZedGraph.PointPairList();
            ZedGraph.LineItem myCurve2;
            ZedGraph.ErrorBarItem myCurve21;



            list11.Add(freq, PhaxyERRmin, PhaxyERRmax);//1
            list1.Add(freq, Phaxy);

            myCurve11 = zedGraphControl2.GraphPane.AddErrorBar("PhaxyERR", list11, Color.DarkGreen);//2
            myCurve1 = zedGraphControl2.GraphPane.AddCurve("Phaxy", list1, Color.DarkGreen, ZedGraph.SymbolType.TriangleDown);
            zedGraphControl2.GraphPane.XAxis.Type = AxisType.Log;
            zedGraphControl2.GraphPane.YAxis.Scale.Max = 90;
            zedGraphControl2.GraphPane.YAxis.Scale.Min = 0;
            /****************由于数据异常，这两根曲线的误差棒不显示，去掉这四行注释就加上误差棒了*******************/
            
            list21.Add(freq, PhayxERRmin, PhayxERRmax);//3
            list2.Add(freq, Phayx);
            myCurve21 = zedGraphControl2.GraphPane.AddErrorBar("PhayxERR", list21, Color.Red);//4
            myCurve2 = zedGraphControl2.GraphPane.AddCurve("Phayx", list2, Color.Red, ZedGraph.SymbolType.Triangle);
            zedGraphControl2.GraphPane.XAxis.Type = AxisType.Log;
            zedGraphControl2.GraphPane.YAxis.Scale.Max = 90;
            zedGraphControl2.GraphPane.YAxis.Scale.Min = 0;

            zedGraphControl2.AxisChange();
            zedGraphControl2.Refresh();
            
        }

        private void OutForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            load.Close();
            load = null;
        }

    }
}
